import time
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from taobao_check.tools import click_by_js, run_async
from .base import BaseBrush


class BaseWolf(BaseBrush):
    service = False

    def __init__(self, name, key, config, tab, service=False):
        super().__init__(name, key, config, tab)
        self.service = service
        # self.timeout = 3
        # self.refresh_time = 2

    def login(self):
        if self.browser.current_url != self.login_url:
            self.browser.get(self.login_url)

        self.enter_text((By.CSS_SELECTOR, "input[type='text']"), self.username)
        self.enter_text((By.CSS_SELECTOR, "input[type='password']"), self.password)

        # 勾选服务协议
        if self.service:
            checkbox = self.find_element((By.CSS_SELECTOR, "span.el-checkbox__inner"))
            click_by_js(self.browser, checkbox)

        self.click_button((By.CSS_SELECTOR, "button[type='button']"))

        self.wait_login()

    def check_user(self):
        try:
            check_button = (By.CSS_SELECTOR, "button.el-button.el-button--success.el-button--mini")
            self.click_button(check_button)
        except TimeoutException:
            self.print_log("没有买号.", debug=True)
            self.sleep(self.refresh_time)
            return False
        time.sleep(1)
        self.print_log("发现待验买号.")
        userform = self.find_element((By.CSS_SELECTOR, "form.lookbuyer"))
        username = userform.find_element_by_css_selector("div.el-form-item__content").get_attribute('textContent')
        username = username.strip()
        self.print_log(f"当前待验买号为：【{username}】")
        start = time.time()
        result = self.check.check_user(username, self.print_log)
        self.print_log(f"验号结果为: {'通过' if result else '不通过'}. 用时: {time.time()-start:.2f}s")

        divs = self.browser.find_elements_by_css_selector("div.dialog-footer")
        buttons = []
        for div in divs:
            buttons = div.find_elements_by_tag_name("button")
            if len(buttons) == 4:
                break

        assert len(buttons) == 4

        rematch_button = buttons[2]
        start_button = buttons[3]
        if result:
            click_by_js(self.browser, start_button)
            self.print_log("开始任务.\n")
        else:
            click_by_js(self.browser, rematch_button)
            self.print_log("开始重新匹配买号.\n")
        return True


class BlackWolf(BaseWolf):
    login_url = "http://shangjia.nbzl1388.cn/#/login"
    index_url = "http://shangjia.nbzl1388.cn/#/index"
    check_url = "http://shangjia.nbzl1388.cn/#/taskList/taskListSales"

    def __init__(self, config, tab):
        super().__init__("黑野狼", "black_wolf", config, tab, True)


class FishPond(BaseWolf):
    login_url = "http://seller.sxx1688.cn/#/login"
    index_url = "http://seller.sxx1688.cn/#/index"
    check_url = "http://seller.sxx1688.cn/#/taskList/taskListSales"

    def __init__(self, config, tab):
        super().__init__("上下鱼塘", "fish_pond", config, tab, False)


class Taojiji(BaseWolf):
    # login_url = "http://seller.tjj918.com/#/login"
    # index_url = "http://seller.tjj918.com/#/index"
    # check_url = "http://seller.tjj918.com/#/taskList/taskListSales"
    login_url = "http://seller.tjj918.top/#/login"
    index_url = "http://seller.tjj918.top/#/index"
    check_url = "http://seller.tjj918.top/#/taskList/taskListSales"

    def __init__(self, config, tab):
        super().__init__("陶吉吉", "taojiji", config, tab, False)


class SeaPond(BaseWolf):
    login_url = "http://seller.dahaiyt8.xyz/#/login"
    index_url = "http://seller.dahaiyt8.xyz/#/index"
    check_url = "http://seller.dahaiyt8.xyz/#/taskList/taskListSales"

    def __init__(self, config, tab):
        super().__init__("大海鱼塘", "sea_pond", config, tab, False)
